# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message: mysql数据导mongo

from MysqlTools import MysqlTools
import pymongo
import json
import sys
import time
import hashlib
try:
    if sys.version_info.major<3:
        from urllib import quote, unquote
    else:
        from urllib.parse import quote, unquote
except:
    pass
import base64
# 用于auth加密与解密
# 密钥
# authkey = "ulei"
# 异或算法
xorStr = lambda ss,cc: ''.join(chr(ord(s)^ord(cc)) for s in ss)
# 方法
def encrypt_auth(data,key=None):
    """数据加密"""
    enStr = data
    for cc in key:
        enStr = xorStr(enStr,cc)
    if isinstance(enStr, str):
        enStr = enStr.encode("utf-8")
    return quote(base64.b64encode(enStr))

def get_md5(str):
    '''获取md5加密电子指纹'''
    md5 = hashlib.md5()
    md5.update(str)
    return md5.hexdigest()

class CarsGroupCollection:

    def __init__(self, **kwargs):
        self.sqldb = MysqlTools(db_config=kwargs.get("db_config"), type='dict')
        self.sql_table = kwargs.get("sql_table")
        self.mongocli = pymongo.MongoClient(host='localhost', port=27017)
        self.mongodb = self.mongocli['nissan']
        self.mongocol = self.mongodb['{}'.format(kwargs.get("mongo_collection"))]

    def cars_group(self):
        all_sql_data = self.sqldb.select_all('select * from {}'.format(sql_table))
        _list = []
        for sql_data in all_sql_data:
            cid = sql_data.get("cid")
            num = sql_data.get("num")
            brandCode = sql_data.get("brandCode")
            label = sql_data.get("label")
            _dict = {
                "cid": cid,
                "key": brandCode+cid,
                "mcid": encrypt_auth(data=json.dumps({'cid':cid}), key=brandCode),
                "num": num,
                "brandCode": brandCode,
                "imagePath": "",
                "label": label,
                "extra_info": "",
                "updatetime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                "createtime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                "status": "1",
            }
            _list.append(_dict)
            if len(_list) == 1000:
                x = self.mongocol.insert_many(_list)
                print("提交")
                _list = []
        self.mongocol.insert_many(_list)

    def cars_vin(self):
        start = 0
        step = 1000000
        while True:
            all_sql_data = self.sqldb.select_all('select * from {} limit {}, {}'.format(self.sql_table, start, step))
            start += step
            _len = len(all_sql_data)
            print(_len)
            _list = []
            for sql_data in all_sql_data:
                vin = sql_data.get("vin")
                shortvin = sql_data.get("shortvin")
                name = sql_data.get("name")
                brandCode = sql_data.get("brandCode")
                year = sql_data.get("year")
                maindata = sql_data.get("maindata")
                subdata = sql_data.get("subdata")
                formatdata = sql_data.get("formatdata")
                code = sql_data.get("code")
                imgname = sql_data.get("imgname")
                extra_info = sql_data.get("extra_info")
                updatetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                createtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                status = '1'

                _dict = {
                    "vin": vin,
                    "shortvin": shortvin,
                    "name": name,
                    "brandCode": brandCode,
                    "year": year,
                    "maindata": json.loads(maindata),
                    "subdata": json.loads(subdata),
                    "formatdata": json.loads(formatdata),
                    "code": code,
                    "mcid": encrypt_auth(json.dumps({'code': code}), brandCode),
                    "imgname": imgname,
                    "extra_info": extra_info,
                    "updatetime": updatetime,
                    "createtime": createtime,
                    "status": status,
                }
                _list.append(_dict)
                if len(_list) == 10000:
                    x = self.mongocol.insert_many(_list)
                    _list = []
            if len(_list) != 0:
                self.mongocol.insert_many(_list)

            if _len < step:
                break

    def cars_structure(self):
        base_path = ''
        all_sql_data = self.sqldb.select_all('select * from {}'.format(self.sql_table))
        _list = []
        for sql_data in all_sql_data:
            cid = sql_data.get("cid")
            num = sql_data.get("num")
            maingroup = sql_data.get("maingroup")
            subgroup = sql_data.get("subgroup")
            mid = sql_data.get("mid")
            brandCode = sql_data.get("brandCode")
            name = sql_data.get("name")
            description = sql_data.get("description")
            model = sql_data.get("model")
            condition = sql_data.get("condition")
            imgName = sql_data.get("imgName")
            fd = open(base_path+imgName+'.png', 'rb')
            imageLarge = get_md5(fd.read())
            imgPoints = sql_data.get("imgPoints")
            logo = sql_data.get("logo")
            extra_info = sql_data.get("extra_info")
            updatetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            createtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            status = sql_data.get("status")
            _dict = {
                "cid": cid,
                "key": brandCode+cid+num+'1',
                "level": 1,
                "has_child": 0,
                "p_name": '',
                "c_name": name,
                "full_path": '/{}'.format(name),
                "mcid": encrypt_auth(json.dumps({'cid':cid}), brandCode),
                "num": num,
                "maingroup": maingroup,
                "subgroup": subgroup,
                "mid": mid,
                "brandCode": brandCode,
                "name": name,
                "description": description,
                "model": model,
                "condition": condition,
                "imgName": imgName,
                "imageLarge": imageLarge,  #
                "imgPoints": json.loads(imgPoints),
                "logo": logo,
                "extra_info": extra_info,
                "updatetime": updatetime,
                "createtime": createtime,
                "status": status,
            }
            _list.append(_dict)
            if len(_list) == 10000:
                x = self.mongocol.insert_many(_list)
                _list = []
        if len(_list) != 0:
            self.mongocol.insert_many(_list)

    def cars_info(self):
        all_sql_data = self.sqldb.select_all("select * from {}".format(self.sql_table))
        for sql_data in all_sql_data:
            pass

    def cars_part(self):
        def get_format_data():
            sql = '''select * from {}'''.format(self.sql_table)
            all_data = self.sqldb.select_all(sql)
            mid_list = [i.get("mid") for i in all_data]
            _dict = {}
            for i in all_data:
                mid = i.get("mid")
                if mid not in _dict:
                    _dict[mid] = [i]
                else:
                    _dict[mid].append(i)
            return mid_list, _dict
        mid_list, all_data = get_format_data()
        base_path = ''
        _list = []
        for mid in mid_list:
            part_list = all_data.get(mid)
            one_part = part_list[0]
            fd = open(base_path + one_part.get("mid") + '.png', 'rb')
            _dict = {
                "key": one_part.get("brandCode")+one_part.get("cid")+one_part.get("num"),
                "cid": one_part.get("cid"),
                "mcid": encrypt_auth(json.dumps({'cid': one_part.get("cid")}), one_part.get("brandCode")),
                "num": one_part.get("num"),
                "maingroup": one_part.get("maingroup"),
                "subgroup": one_part.get("subgroup"),
                "brandCode": one_part.get("brandCode"),
                "img": {},
                "imageLarge": get_md5(fd.read()),
                "part_detail": [],
                "status": "1",
                "updatetime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                "createtime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            }
            for part in part_list:
                part_dict = {
                    "pnum": part.get("pnum"),
                    "itid": part.get("itid"),
                    "psort": part.get("psort"),
                    "pid": part.get("pid"),
                    "label": part.get("label"),
                    "remark": part.get("remark"),
                    "quantity": part.get("quantity"),
                    "model": part.get("model"),
                    "iscomt": '',
                    "has_article": '',
                    "isreplace": '',
                    "condition": part.get("condition"),
                    "extra_info": part.get("extra_info")
                }
                _dict['part_detail'].append(part_dict)
            _list.append(_dict)
            if len(_list) == 10000:
                x = self.mongocol.insert_many(_list)
                _list = []
        if len(_list) != 0:
            x = self.mongocol.insert_many(_list)


if __name__ == '__main__':
    db_config = dict(
        host = "127.0.0.1",
        port = 3306,
        user = "root",
        passwd = "123456",
        db = "nissan_new",
        charset = "utf8"
    )
    sql_table = 'cars_vin_info'
    mongo_collection = 'cars_vin_info'
    tools = CarsGroupCollection(db_config=db_config, sql_table=sql_table, mongo_collection=mongo_collection)
    # tools.cars_group()
    tools.cars_vin()